package be.rugbyclubleuven.rachel.controller.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Utility class for the controller.
 */
public class ControllerUtils {

	/**
	 * Empty constructor.
	 */
	private ControllerUtils() {
	}

	public static final String DATA = "data";
	public static final String SUCCESS = "success";
	public static final String MESSAGE = "message";
	public static final String TOTAL = "total";

	/**
	 * Gets a map holding the success property.
	 * @param success indicates success or not
	 * @return a map holding the success property
	 */
	public static Map<String, Object> getSuccessMap(boolean success) {
		Map<String, Object> modelMap = new HashMap<String, Object>(1);
		modelMap.put(SUCCESS, success);
		return modelMap;
	}

	/**
	 * Gets a map holding the data and success (true) property.
	 * @param object the object to put in the data property
	 * @return a map holding the data and success property
	 */
	public static Map<String, Object> getObjectMap(Object object) {
		Map<String, Object> modelMap = new HashMap<String, Object>(2);
		modelMap.put(DATA, object);
		modelMap.put(SUCCESS, true);
		return modelMap;
	}

	/**
	 * Gets a map holding the data,total and success (true) property.
	 * @param list the list to put in the data property, its size is put in the total property
	 * @return a map holding the data, total and success property
	 */
	public static <T> Map<String, Object> getListMap(List<T> list) {
		Map<String, Object> modelMap = new HashMap<String, Object>(3);
		if (list != null) {
			modelMap.put(TOTAL, list.size());
		}
		modelMap.put(DATA, list);
		modelMap.put(SUCCESS, true);
		return modelMap;

	}

	/**
	 * Gets a map holding the error messages
	 * @param msg the error message to be put in the message property
	 * @return a map holding the success, data and message property
	 */
	public static Map<String, Object> getModelMapError(String msg) {
		Map<String, Object> modelMap = new HashMap<String, Object>(3);
		modelMap.put(MESSAGE, msg);
		modelMap.put(SUCCESS, false);
		modelMap.put(DATA, new ArrayList<Object>());
		return modelMap;
	}

}
